//
//  main.m
//  OC02_homework
//
//  Created by Hanlingyu on 16/7/5.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
#pragma mark - 不可变字符串
#if 0
    /** 初始化(init) */
    
    NSString *str = [[NSString alloc] initWithFormat:@"lanou3g"];
    NSLog(@"%@", str);
    
    /** 便利构造器(string) */
    NSString *str1 = [NSString stringWithFormat:@"lanou3g"];
    NSLog(@"%@", str1);
    
    
    /** 获取字符串长度(length) */
    NSLog(@"%ld", str1.length);
    
    
    /** 获取字符串中字符(character) */
    NSLog(@"%c", [str characterAtIndex:2]);
    
    
    /** 字符串是否相等(equal) */
    BOOL isEqual = [str isEqualToString:str1];
    if (isEqual) {
        NSLog(@"两个字符串相等");
    } else {
        NSLog(@"两个字符串不相等");
    }
    
    
    /** 获取子串(sub) */
    NSString *subStr = [str substringFromIndex:2];
    NSString *subStr1 = [str substringToIndex:3];
    NSRange range = {0, 3};
    NSString *subStr2 = [str substringWithRange:range];
    NSLog(@"subStr: %@\n, subStr1: %@\n, subStr2: %@\n", subStr, subStr1, subStr2);
    
    
    
    /** 拼接字符串(append) */
    
    NSLog(@"%@", [str stringByAppendingString:str1]);
    
    /** 替换字符串(replace) */
    NSString *str3 = [NSString stringWithFormat:@"学院"];
    NSLog(@"%@", [str3 stringByReplacingOccurrencesOfString:str withString:str1]);
    
    
    /** 字符串对象转化为int类型(int) */
    
    NSLog(@"%d", str.intValue);
    
    /** 字符串对象转化为其他数据类型, 例如double, float, BOOL等. */
    NSLog(@"%.2lf", str.doubleValue);
    NSLog(@"%.2f", str.floatValue);
    NSLog(@"%x", str.boolValue);
    
    
    /** 字符串全部大写(uppercase) */
    NSLog(@"%@", [str uppercaseString]);
    
    
    /** 字符串全部小写(lowercase) */
    NSString *str4 = @"LANOU";
    NSLog(@"%@", [str4 lowercaseString]);
    
    
    /** 字符串首字母大写(capitalized) */
    NSLog(@"%@", [str capitalizedString]);
    

    /** 是否以指定字符串为前缀(prefix) */
    BOOL isPrefix = [str hasPrefix:@"lan"];
    NSLog(@"%x", isPrefix);
    
    
    
    /** 是否以指定字符串为后缀(suffix) */
    BOOL isSuffix = [str hasSuffix:@"3g"];
    NSLog(@"%x", isSuffix);
    
    
    /** 练习: http://www.lanou3g.com/icon.png 判断上述字符串中是否以“png”结尾，如果是就替换成“jpg”，如果不是，就拼接”.jpg”。*/
    
    NSString *string = @"http://www.lanou3g.com/icon.png";
    NSString *string1;
    if ([string hasSuffix:@"png"]) {
        string1 = [string stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
    } else {
        string1 = [string stringByAppendingString:@".jpg"];
    }
    NSLog(@"%@", string1);
#endif
    
    
#pragma mark - 可变字符串
    
    /** 初始化方法 */
    
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"http://www.lanou3g.com/icon.png"];
    NSLog(@"%@", str);
    /** 字面量 */
    NSMutableString *str1 = @"http://www.lanou3g.com/icon.png".mutableCopy;
    
    /** 便利构造器(string) */
    NSMutableString *str3 = [NSMutableString stringWithFormat:@"http://www.lanou3g.com/icon.png"];
    NSMutableString *str4 = [NSMutableString stringWithString:str3];
    
    
    // 1.330184198501184115 处理字符串, 取出他的出生年月, 并计算出年龄(不用具体)
    // 输出格式 1985年01月18日 年龄:31岁
    
    
    NSString *string = @"330184198501184115";
// 年月日的范围
    NSRange yearRange = {6, 4};
    NSRange mouthRange = {10, 2};
    NSRange dayRange = {12, 2};
// 取出年月日的字符串
    NSString *yearStr = [string substringWithRange:yearRange];
    NSString *mouthStr = [string substringWithRange:mouthRange];
    NSString *dayStr = [string substringWithRange:dayRange];
// 计算年龄
    NSInteger age = 2016 - yearStr.integerValue;
// 总结出输出的字符串格式
    NSString *outPutStr = [NSString stringWithFormat:@"%@年%@月%@日 年龄: %ld", yearStr, mouthStr, dayStr, age];
    NSLog(@"%@", outPutStr);
    
    
    
    
    
    return 0;
}
